package com.houdoudou.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.houdoudou.bean.Financer;

public class FinancerDao {

	 Connection conn=null;
	   
		public FinancerDao(Connection conn) {
			super();
			this.conn = conn;
		}
		
		public  boolean create( Financer financer ) throws SQLException{	
			boolean result=false;
			  PreparedStatement pstm=null; 
			   Statement  stm=null;
			   ResultSet rs=null; 	
			   String sql=null;
		        try {
		        	sql = "insert  into financer (telnum,secret,type) values (?,?,?)";
		        	pstm=conn.prepareStatement(sql);
		        	pstm.setString(1, financer.getTelnum());
		        	pstm.setString(2, financer.getSecret());
		        	pstm.setInt(3, financer.getType());
					pstm.executeUpdate();
					result=true;
				} finally{
					ConnFactory.closeStatment(pstm, pstm, rs);
				}			
			return result;
		}
		
		public  Financer delete( String telnum) throws SQLException{	
			Financer financer = new Financer();
			  PreparedStatement pstm=null; 
			   Statement  stm=null;
			   ResultSet rs=null; 	
			   String sql=null;
		        try {
		        	sql = "delete  from financer where telnum="+telnum+"";		        	
					stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
					stm.executeUpdate(sql);
					
				} finally{
					ConnFactory.closeStatment(pstm, pstm, rs);
				}			
			return financer;
		}
		
		public  int update( Financer financer ) throws SQLException{	
			int result=0;
			  PreparedStatement pstm=null; 
			   Statement  stm=null;
			   ResultSet rs=null; 	
			   String sql=null;
		        try {
		        	sql = "update financer set secret = ?,type = ?  where telnum = ?";
		        	pstm=conn.prepareStatement(sql);
		        	pstm.setString(1, financer.getSecret());
		        	pstm.setInt(2, financer.getType());
		        	pstm.setString(3, financer.getTelnum());
					
					result=pstm.executeUpdate();
					
				} finally{
					ConnFactory.closeStatment(pstm, pstm, rs);
				}			
			return result;
		}
		
		public  Financer selectByTelnum( String telnum) throws SQLException{	
			Financer financer = new Financer();
			  PreparedStatement pstm=null; 
			   Statement  stm=null;
			   ResultSet rs=null; 	
			   String sql=null;
		        try {
		        	sql = "select * from financer where telnum="+telnum+"";		        	
					stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
					rs=stm.executeQuery(sql);
					if(rs.next()){
						financer.setTelnum(rs.getString("telnum"));
						financer.setSecret(rs.getString("secret"));
						financer.setType(rs.getInt("type"));
					}
				} finally{
					ConnFactory.closeStatment(pstm, pstm, rs);
				}			
			return financer;
		}
	
		public List< Financer> selectAll( ) throws SQLException{	
			List< Financer> financers= new ArrayList<Financer>();
			  PreparedStatement pstm=null; 
			   Statement  stm=null;
			   ResultSet rs=null; 	
			   String sql=null;
		        try {
		        	sql = "select * from financer ";		        	
					stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
					rs=stm.executeQuery(sql);
					while(rs.next()){
						Financer financer = new Financer();
						financer.setTelnum(rs.getString("telnum"));
						financer.setSecret(rs.getString("secret"));
						financer.setType(rs.getInt("type"));
						financers.add(financer);
					}
				} finally{
					ConnFactory.closeStatment(pstm, pstm, rs);
				}			
			return financers;
		}	
}
